Backend series chai aur code
Basic backend setup
Install nodejs first and then continue below
create a empty folder
open integrated termial of the folder then write
npm init
or
npm init -y
-y generates a new package.json file with default values, bypassing the interactive prompts.
The init package would look like this
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.21.1"
}
}
now we create our own run script in this init package
in the script section init package write "start":"node server.js"
server.js will be our js file so the package.json script array would look like this
"scripts": {
"start":"node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
server.js
now create a server.js file
inside the server.js we write
import express from 'express'
const app = express()
we can also write const express = require('express'); but we use ESM method and not cjs to know more diffrence read these note 0.Node 3.Express
as we are are writing the the esm method we have to specify this in package.json file with "type":"module"
write a basic code in server.js
import express from 'express'
const app = express()
app.get("/",(req,res)=>{
res.send("this is page")
})
app.listen(3000)
We write the port in .env and import it in the JS file to easily configure and change it across different environments without modifying the code.
for our backend we are using port 3000 but it isa a private variable for our data for example our databse uri is also private so we save it in .env file so not to push it on github or share anywhere we can write this env in .gitignore file
to use this we
install
npm i dotenv
then create a .env file and write PORT=3000
as we are using the type:module as import format we import the port in server.js as
import express from "express"
import 'dotenv/config'
const app = express()
const port=process.env.PORT
app.use("/",(req,res)=>{
res.send("this is using env")
})
app.listen(port)
or
for the cjs type import do this
require('dotenv').config()
app.listen(process.env.PORT,()=>{
console.log('listening')
})
create a basic backend to send the data as json
import express from "express"
const app = express()
import 'dotenv/config'
const port=process.env.PORT ;
app.get("/",(req,res)=>{
res.send("this is using env")
})
app.get("/jokes",(req,res)=>{
const jokes = [
{
id: 1,
title: "1st Joke",
content: "Why don't skeletons fight each other? They don't have the guts."
},
{
id: 2,
title: "2nd Joke",
content: "Why don’t some couples go to the gym? Because some relationships don’t work out."
},
{
id: 3,
title: "3rd Joke",
content: "Why did the scarecrow win an award? Because he was outstanding in his field."
},
{
id: 4,
title: "4th Joke",
content: "I told my wife she was drawing her eyebrows too high. She looked surprised."
},
{
id: 5,
title: "5th Joke",
content: "I’m reading a book about anti-gravity. It’s impossible to put down."
}
];
res.send(jokes);
})
app.listen(port)
Reference
Next lesson connecting this backend with frontend
5A.Connecting Express Server with Axios (vite)